<?php
// +----------------------------------------------------------------------
// | Description: 数据库备份类
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\ApiCommon;

class DbBackup extends ApiCommon
{
    public function _initialize()
    {
        parent::_initialize();
        $this -> abort(!$this -> isClient, 'Permission denied', 403);
    }

    use \app\Traits\DbBackupTrait;

    public function index()
    {
        
        $type = input("tp");
        $name = input("name");
        $sql = new \org\Baksql(\think\Config::get("database"));
        switch ($type) {
            case "backup": //备份
                $info = $sql->backup();
                break;
            case "dowonload": //下载
                $data = $sql->downloadFile($name);
                $this->jsonData($data, $sql->getError());
                break;
            case "restore": //还原
                $info = $sql->restore($name);
                // 由于触发器/存储过程/事件不会自动备份  所以这里需要自动导入一下
                $this->otherData();
                break;
            case "del": //删除
                $info = $sql->delfilename($name);
                break;
            default: //获取备份文件列表
                $this->dataList = $sql->get_filelist();
                $this->PageList();
                $info = $this->finalDataList;
                break;
        }
        $this->jsonData($info);
    }

    /**
     * 除了一般表数据之外的 触发器 存储过程 和事件的还原(本项目 这些都写在php程序中  不能直接写在数据库中)
     *
     * @return void
     */
    public function otherData()
    {
        $Triggers = new \app\mysql\Triggers();
        $Triggers->runAll();
        $Procedure = new \app\mysql\Procedure();
        $Procedure->runAll();
        $Event = new \app\mysql\Event();
        $Event->runAll();
    }
}
